<?php

add_action( 'admin_init', 'scm_blog_description_options_init' );
add_action( 'admin_menu', 'scm_blog_description_add_page' );

/**
 * Init plugin options to white list our options
 */
function scm_blog_description_options_init(){
	register_setting( 'scm_blog_descriptions_options', 'scm_blog_descriptions_options', 'scm_blog_descriptions_validate' );
}

/**
 * Load up the menu page
 */
function scm_blog_description_add_page() {
	add_options_page( __( 'Site Description Swapper Options' , 'scm_description_swapper' ) , __( 'Blog Description Swapper Options' , 'scm_description_swapper' ), 'administrator', 'scm_description_swapper', 'scm_description_swapper_do_page' );
}

/**
 * Create the options page
 */
function scm_description_swapper_do_page() {
	$i = 1;
	if ( ! isset( $_REQUEST['updated'] ) )
		$_REQUEST['updated'] = false

	?>
	<div class="wrap">
		<?php screen_icon(); echo "<h2>"  .  __( 'Site Description Swapper Options' , 'scm_description_swapper' ) . "</h2>"; ?>

		<?php if ( false !== $_REQUEST['updated'] ) : ?>
		<div class="updated fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
		<?php endif; ?>

		<form method="post" action="options.php">
			<?php settings_fields( 'scm_blog_descriptions_options' ); ?>
			<?php $options = get_option( 'scm_blog_descriptions_options' ); ?>
			<?php wp_nonce_field( 'blog_description_nonce', 'blog_description_nonce' ); ?>
			<table class="form-table">

				<tr valign="top"><th scope="row"><?php _e( 'Blog Description' ); ?></th>
					<td>
						<input id="scm_blog_descriptions_options[]" class="regular-text" type="text" disabled="disabled" name="scm_blog_descriptions_options[]" value="<?php esc_attr_e( get_option('blogdescription') ); ?>" />
						<label class="description" for="scm_blog_descriptions_options[]"><?php _e( 'This is your default blog description' ); ?></label>
					</td>
				</tr>
				<?php if( !empty( $options )) : ?>
					<?php foreach($options as $option): ?>
						<?php if( get_option( 'blogdescription' ) != $option) : ?>
							<tr valign="top"><th scope="row"><?php echo __( 'Blog Description' ) . ' ' . $i; ?></th>
								<td>
									<input id="scm_blog_descriptions_options[]" class="regular-text" type="text" name="scm_blog_descriptions_options[]" value="<?php esc_attr_e( stripslashes( $option ) ); ?>" />
									<label class="description" for="scm_blog_descriptions_options[]"><?php _e( 'This is an additional blog description leave empty to delete' ); ?></label>
								</td>
							</tr>
							<?php $i++; ?>
						<?php endif; ?>
					<?php endforeach; ?>
				<?php endif; ?>
				<tr valign="top"><th scope="row"><?php _e( 'New Blog Description' ); ?></th>
					<td>
						<input id="scm_blog_descriptions_options[]" class="regular-text" type="text" name="scm_blog_descriptions_options[]" value=""	 />
						<label class="description" for="scm_blog_descriptions_options[]"><?php _e( 'This is an additional blog description' ); ?></label>
					</td>
				</tr>
				
			</table>

			<p class="submit">
				<input type="submit" class="button-primary" value="<?php _e( 'Save Options' ); ?>" />
			</p>
		</form>
	</div>
	<?php
}

/**
 * Sanitize and validate input. Accepts an array, return a sanitized array.
 */
function scm_blog_descriptions_validate( $inputs ) {
	if( !wp_verify_nonce( $_POST['blog_description_nonce'] , 'blog_description_nonce')) return false;

	// Say our text option must be safe text with no HTML tags
	if( empty( $inputs ) )	return $inputs;
	
	$sanitized_inputs = array();
	foreach( (array)$inputs as $input){
		if( !empty( $input ) )
			$sanitized_inputs[] = wp_filter_nohtml_kses( $input );	
	
	}// end foreach

	return $sanitized_inputs;
}
?>